using DeviceDataGenerator;
using System;

namespace DeviceDataGenerator
{
    /// <summary>
    /// 测试记录生成演示程序
    /// </summary>
    public class TestRecordDemo
    {
        public static void RunDemo()
        {
            Console.WriteLine("=== DeviceCommons 测试记录生成演示 ===\n");

            // 创建模拟的测试数据
            var stats = new PerformanceStats();
            var settings = DefaultConfig.GetDefault();
            var testStartTime = DateTime.Now.AddMinutes(-10); // 模拟10分钟前开始的测试

            Console.WriteLine("1. 生成模拟测试数据...");
            GenerateMockTestData(stats);

            Console.WriteLine("2. 创建测试记录生成器...");
            var generator = new TestRecordGenerator(stats, settings, testStartTime);

            Console.WriteLine("3. 生成测试记录文档...");
            var filePath = generator.GenerateTestRecord();

            Console.WriteLine($"\n✅ 测试记录生成完成！");
            Console.WriteLine($"📄 Markdown报告: {filePath}");
            Console.WriteLine($"📊 JSON数据文件: {Path.ChangeExtension(filePath, null).Replace("TestRecord_", "TestData_")}.json");
            Console.WriteLine($"📁 文件保存目录: {Path.GetDirectoryName(filePath)}");

            Console.WriteLine("\n🎯 生成的文档包含:");
            Console.WriteLine("  - 测试配置信息");
            Console.WriteLine("  - 总体测试结果统计");
            Console.WriteLine("  - 系统资源使用情况");
            Console.WriteLine("  - 数据大小分段统计");
            Console.WriteLine("  - 加密和压缩性能对比");
            Console.WriteLine("  - 性能趋势分析");
            Console.WriteLine("  - 结论和优化建议");

            Console.WriteLine("\n📝 使用方法:");
            Console.WriteLine("  在实际性能测试中，按 Ctrl+C 退出时会自动生成测试记录");
            Console.WriteLine("  测试记录保存在程序目录的 TestRecords 文件夹中");

            Console.WriteLine("\n演示完成！");
        }

        /// <summary>
        /// 生成模拟测试数据
        /// </summary>
        private static void GenerateMockTestData(PerformanceStats stats)
        {
            var random = new Random();
            var scenarios = new[]
            {
                (messageSize: 500, description: "小消息", testCount: 50),
                (messageSize: 5000, description: "中等消息", testCount: 30),
                (messageSize: 50000, description: "大消息", testCount: 20),
                (messageSize: 200000, description: "超大消息", testCount: 10)
            };

            foreach (var (messageSize, description, testCount) in scenarios)
            {
                var combinations = new[]
                {
                    (useEncryption: false, useCompression: false),
                    (useEncryption: true, useCompression: false),
                    (useEncryption: false, useCompression: true),
                    (useEncryption: true, useCompression: true)
                };

                foreach (var (useEncryption, useCompression) in combinations)
                {
                    for (int i = 0; i < testCount; i++)
                    {
                        // 基础解析时间
                        var baseParseTime = messageSize switch
                        {
                            <= 1000 => random.NextDouble() * 0.2 + 0.1,
                            <= 10000 => random.NextDouble() * 0.5 + 0.3,
                            <= 100000 => random.NextDouble() * 1.5 + 1.0,
                            _ => random.NextDouble() * 3.0 + 2.0
                        };

                        // 加密增加15%开销
                        if (useEncryption) baseParseTime *= 1.15;

                        // 压缩增加10%开销
                        if (useCompression) baseParseTime *= 1.10;

                        // 压缩减少30%数据大小
                        var adjustedMessageSize = useCompression ? (int)(messageSize * 0.7) : messageSize;

                        var result = new TestResult
                        {
                            Config = new TestConfig 
                            { 
                                DeviceName = $"{description}_Device_{i}",
                                UseEncryption = useEncryption,
                                UseCompression = useCompression
                            },
                            BuildTime = (long)(random.NextDouble() * 500000),
                            SerializeTime = (long)(random.NextDouble() * 1000000),
                            ParseTime = (long)(baseParseTime * System.Diagnostics.Stopwatch.Frequency / 1000),
                            MessageSize = adjustedMessageSize,
                            Success = true,
                            CpuUsagePercent = random.NextDouble() * 40 + 20,
                            MemoryUsageBytes = random.NextInt64(80 * 1024 * 1024, 200 * 1024 * 1024),
                            Timestamp = DateTime.Now
                        };

                        stats.AddResult(result);
                    }
                }
            }

            Console.WriteLine($"   ✓ 已生成 {stats.TotalTests} 个测试样本");
            Console.WriteLine($"   ✓ 覆盖 4 种数据大小分段");
            Console.WriteLine($"   ✓ 包含 4 种加密压缩状态组合");
        }
    }
}